﻿using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Service;
using Logistics.Utility;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using Logistics.Model.QueryModel;
using DapperExtensions;
using Logistics.Model.Response;

namespace Logistics.WebApi.Controllers
{
    [MyAuthority]
    public class NoticeController: ApiController
    {
        private NoticeService service = new NoticeService();
        private CompanyNoticeService cnService = new CompanyNoticeService();
        private AccountService accountService = new AccountService();
        private CompanyService companyService = new CompanyService();
        [HttpPost]
        public ResponseModel GetPage(NoticeQuery query)
        {
            var noticeQuery = new List<IPredicate>();
            var resultmod = new ResponseModel();
            try
            {
                if (query.release_time1 > 0)
                    noticeQuery.Add(Predicates.Field<wuliu_wlcompany_notice>(f => f.release_time, Operator.Ge, query.release_time1));
                if (query.release_time2 > 0)
                    noticeQuery.Add(Predicates.Field<wuliu_wlcompany_notice>(f => f.release_time, Operator.Gt, query.release_time2));
                noticeQuery.Add(Predicates.Field<wuliu_wlcompany_notice>(f => f.idelete, Operator.Eq, 1));
      
                var predicate = Predicates.Group(GroupOperator.And, noticeQuery.ToArray());
                IList<ISort> sorts = new List<ISort>() { 
                    Predicates.Sort<wuliu_notice>(p=>p.id)
                };
                var noticeList = service.GetPageList(predicate, sorts, query.page - 1, query.pagesize);
                resultmod.mod = noticeList;
                if (noticeList.TotalNum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ResponseModel DeleteNotice(int id)
        {
            var responseModel = new ResponseModel();
            try
            {
                var noticemod = service.GetWlNotice(id);
                wuliu_wlcompany_notice mod = new wuliu_wlcompany_notice();
                mod.id = noticemod.id;
                mod.idelete = 10;
                responseModel.mod = service.DeleteNotice(mod);
                responseModel.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }
        [HttpPost]
        public ResponseModel Add(wuliu_notice notice)
        {
            var resultmod = new ResponseModel();
            try
            {
                var id = service.Add(notice);
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
               var compayid = accountService.GetCompanyId(accountId);
                //var company = companyService.GetCompanyByAccountId(accountId);
                if (id > 0)
                {
                    wuliu_wlcompany_notice company_notice = new wuliu_wlcompany_notice();
                    company_notice.idelete = 1;
                    company_notice.iread = 1;
                    company_notice.notice_id = id;
                    company_notice.Operator = accountId.ToString();
                    company_notice.operator_time = DateTime.Now;
                    company_notice.release_time = DateTime.Now;
                    company_notice.title = notice.title;
                    var cid = cnService.Add(company_notice);
                    if (cid > 0)
                    {
                        resultmod.InitResultState(StateEnum.Success);
                    }
                    else
                    {
                        resultmod.InitResultState(StateEnum.Error);
                    }
                }
                else
                {
                    resultmod.InitResultState(StateEnum.Error);
                }

            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        public ResponseModel Get(int id)
        {
            var responseModel = new ResponseModel();
            try
            {
                responseModel.mod = service.Get(id);
                responseModel.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }
    }
}